//
//  DynamicViewController.m
//  UI_Dynamic
//
//  Created by dlios on 15-1-15.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "DynamicViewController.h"
#import "PlayerViewController.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "PlayerSingle.h"
@interface DynamicViewController ()

@end

@implementation DynamicViewController
- (void)dealloc
{
    [_dynamicTableView release];
    [_dynamicArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"Dynamic";
    
    self.dynamicTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style: UITableViewStylePlain];
    
    [self.view addSubview:self.dynamicTableView];
    
    [_dynamicTableView release];
    
    self.dynamicTableView.delegate = self;
    
    self.dynamicTableView.dataSource = self;
    
    
    [self AFN];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.dynamicTableView = nil;
}

- (void)playAudio:(AudioButton *)button
{
    


    PlayerSingle *audioPlayer = [[PlayerSingle alloc] init];
    
    
    
        if (_audioPlayer == nil) {
            _audioPlayer = [[AudioPlayer alloc] init];
        }
    
        if ([_audioPlayer.button isEqual:button]) {
    
            [_audioPlayer play];
    
        } else {
            [_audioPlayer stop];
    
            _audioPlayer.button = button;
    
            _audioPlayer.url = [NSURL URLWithString:@"http://fdfs.xmcdn.com/group5/M08/48/7F/wKgDtVS3cgDQ01V6AJB3Gcm6Xf0350.mp3"];
            
            [_audioPlayer play];

        

        [_audioPlayer play];
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}





- (void)AFN
{
//    afn解析数据
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSLog(@"%d",netWorkManager.isReachable);
    
    
    
    NSString *url_string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/api/1/feed/dynamic?device=android&sign=2&size=15&token=b08a03f905b89044ad08dcb0297b514ef066&uid=20696638"];
    NSLog(@"%@",url_string);
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSLog(@"object ===  %@",responseObject);
        NSMutableDictionary *dyDic = responseObject;
        NSMutableArray *array = [dyDic objectForKey:@"datas"];
        self.dynamicArray = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            DynamicModel *model = [[DynamicModel alloc] init];
            model.albumTitle = [dic objectForKey:@"albumTitle"];
            
            model.albumCover = [dic objectForKey:@"albumCover"];
            
            model.lastUpdateAt = (NSNumber *)[dic objectForKey:@"lastUpdateAt"];
            
            model.trackTitle = [dic objectForKey:@"trackTitle"];
            
            model.avatar = [dic objectForKey:@"avatar"];
            
            model.nickname = [dic objectForKey:@"nickname"];

            model.playPath32 = [dic objectForKey:@"playPath32"];
            
            model.playPath64 = [dic objectForKey:@"playPath64"];
      
            [self.dynamicArray addObject:model];
            
            NSLog(@"time = %@", model.timeline);
        }
        
        [self.dynamicTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败 %@",error);
    }];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    PlayerViewController *player = [[PlayerViewController alloc] init];

    DynamicModel *model = [self.dynamicArray objectAtIndex:indexPath.row];
    
    player.text1 = model.playPath32;
    
    player.title1 = model.albumTitle;
    
    NSURL *url = [NSURL URLWithString:model.albumCover];
    
    player.image = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    
    [player.image sd_setImageWithURL:url];
    
    NSLog(@"model.model.albumCover = %@", url);
    NSLog(@"%@",player.image);
    
//    [self.dynamicTableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:player];
    
    [self presentViewController:navigation animated:YES completion:^{
        
        
    }];


}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetifier = @"reuse";
    DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
    if (cell == nil) {
        cell = [[DynamicTableViewCell alloc] initWithStyle:    UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
    }
    DynamicModel *model = [self.dynamicArray objectAtIndex:indexPath.row];
    NSLog(@"%@", model);
    cell.model = model;
    self.index = 0;
    self.index = indexPath.row;
    [cell.audioButton addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    _audioPlayer.url = [NSURL URLWithString:model.playPath32];
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dynamicArray.count;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
